package com.zycfc.zsf.boot.util.time;

import org.apache.commons.lang3.time.*;
import java.util.*;
import org.apache.commons.lang3.*;
import java.sql.*;
import com.zycfc.zsf.boot.util.text.*;
import java.text.*;
import java.math.*;
import org.slf4j.*;

public class DateUtils
{
    private static final Logger log;
    public static final long MILLIS_PER_SECOND = 1000L;
    public static final long MILLIS_PER_MINUTE = 60000L;
    public static final long MILLIS_PER_HOUR = 3600000L;
    public static final long MILLIS_PER_DAY = 86400000L;
    private static final int[] MONTH_LENGTH;
    public static final String PATTERN_ISO = "yyyy-MM-dd'T'HH:mm:ss.SSSZZ";
    public static final String PATTERN_ISO_ON_SECOND = "yyyy-MM-dd'T'HH:mm:ssZZ";
    public static final String PATTERN_ISO_ON_DATE = "yyyy-MM-dd";
    public static final String PATTERN_ISO_ON_MONTH = "yyyy-MM";
    public static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String PATTERN_DEFAULT_ON_SECOND = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_NO_SEP_ON_SECOND = "yyyyMMddHHmmss";
    public static final String PATTERN_NO_SEP_ON_MINUTE = "yyyyMMddHHmm";
    public static final String PATTERN_NO_SEP_ON_DAY = "yyyyMMdd";
    public static final FastDateFormat ISO_FORMAT;
    public static final FastDateFormat ISO_ON_SECOND_FORMAT;
    public static final FastDateFormat ISO_ON_DATE_FORMAT;
    public static final FastDateFormat ISO_ON_MONTH_FORMAT;
    public static final FastDateFormat DEFAULT_FORMAT;
    public static final FastDateFormat DEFAULT_ON_SECOND_FORMAT;
    public static final FastDateFormat FORMAT_NO_SEP_ON_SECOND;
    public static final FastDateFormat FORMAT_NO_SEP_ON_MINUTE;
    public static final FastDateFormat FORMAT_NO_SEP_ON_DAY;
    
    public static boolean isSameDay(final Date date1, final Date date2) {
        if (date1 != null && date2 != null) {
            final Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);
            final Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            return isSameDay(cal1, cal2);
        }
        throw new IllegalArgumentException("The date must not be null");
    }
    
    public static boolean isSameDay(final Calendar cal1, final Calendar cal2) {
        if (cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        }
        throw new IllegalArgumentException("The date must not be null");
    }
    
    public static boolean isSameTime(final Date date1, final Date date2) {
        return date1.compareTo(date2) == 0;
    }
    
    public static boolean isBetween(final Date date, final Date start, final Date end) {
        if (date == null || start == null || end == null || start.after(end)) {
            throw new IllegalArgumentException("some date parameters is null or dateBein after dateEnd");
        }
        return !date.before(start) && !date.after(end);
    }
    
    public static int getDayOfWeek(final Date date) {
        return get(date, 7);
    }
    
    public static int getDayOfMonth(final Date date) {
        return get(date, 5);
    }
    
    public static int getDayOfYear(final Date date) {
        return get(date, 6);
    }
    
    public static int getWeekOfMonth(final Date date) {
        return get(date, 4);
    }
    
    public static int getWeekOfYear(final Date date) {
        return get(date, 3);
    }
    
    public static int getCurWeekOfYear() {
        final Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(2);
        cal.setTimeInMillis(System.currentTimeMillis());
        return cal.get(3);
    }
    
    public static int getCurMonthOfYear() {
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        return cal.get(2) + 1;
    }
    
    private static int get(final Date date, final int field) {
        Validate.notNull((Object)date, "The date must not be null", new Object[0]);
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(field);
    }
    
    public static boolean isLeapYear(final Date date) {
        return isLeapYear(get(date, 1));
    }
    
    public static boolean isLeapYear(final int y) {
        boolean result = false;
        if (y % 4 == 0 && (y < 1582 || y % 100 != 0 || y % 400 == 0)) {
            result = true;
        }
        return result;
    }
    
    public static int getMonthLength(final Date date) {
        final int year = get(date, 1);
        final int month = get(date, 2) + 1;
        return getMonthLength(year, month);
    }
    
    public static int getMonthLength(final int year, final int month) {
        if (month < 1 || month > 12) {
            throw new IllegalArgumentException("Invalid month: " + month);
        }
        if (month == 2) {
            return isLeapYear(year) ? 29 : 28;
        }
        return DateUtils.MONTH_LENGTH[month];
    }
    
    public static Timestamp getCurTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }
    
    public static String getCurDT() {
        return getCurTime("yyyyMMdd");
    }
    
    public static String getCurTM() {
        return getCurTime("HHmmss");
    }
    
    public static String getCurMonth() {
        return getCurTime("yyyyMM");
    }
    
    public static String getCurTime(final String format) {
        final StringBuilder str = new StringBuilder();
        final Date ca = Calendar.getInstance().getTime();
        final SimpleDateFormat sdf = new SimpleDateFormat(format);
        str.append(sdf.format(ca));
        return str.toString();
    }
    
    public static Date getCurDate() {
        return new Date();
    }
    
    public static java.sql.Date getCurSqlDate() {
        try {
            final Date date = getCurDate();
            final java.sql.Date sqlDate = new java.sql.Date(date.getTime());
            return sqlDate;
        }
        catch (Exception ex) {
            DateUtils.log.error("\u8f6c\u6362\u5f02\u5e38", (Throwable)ex);
            return null;
        }
    }
    
    public static Date getCurYMD() {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        final Date date = new Date();
        final String dateStr = sdf.format(date);
        return stringToDate(dateStr);
    }
    
    public static Timestamp getCurYMDSql() {
        final Timestamp dateSQL = new Timestamp(getCurYMD().getTime());
        return dateSQL;
    }
    
    public static Timestamp getCurYMDSql(final Date date) {
        final Timestamp dateSQL = new Timestamp(date.getTime());
        return dateSQL;
    }
    
    public static Date getCurYYMMDD(final String date) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            return sdf.parse(date);
        }
        catch (ParseException e) {
            DateUtils.log.error("\u5f02\u5e38", (Throwable)e);
            return null;
        }
    }
    
    public static java.sql.Date getCurSqlYMD() {
        try {
            final Date date = getCurYMD();
            final java.sql.Date sqlDate = new java.sql.Date(date.getTime());
            return sqlDate;
        }
        catch (Exception ex) {
            DateUtils.log.error("\u8f6c\u6362\u5f02\u5e38", (Throwable)ex);
            return null;
        }
    }
    
    public static Date getCurYMD(final Date date) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        final String dateStr = sdf.format(date);
        return stringToDate(dateStr);
    }
    
    public static Date getCurYMDHM(final Date date) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        final String dateStr = sdf.format(date);
        return stringToDate(dateStr);
    }
    
    public static Date getCurYMDH(final Date date) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00");
        final String dateStr = sdf.format(date);
        return stringToDate(dateStr);
    }
    
    public static String getSysOptDate() {
        final Calendar date = Calendar.getInstance();
        final Date sysDate = date.getTime();
        return dateToString(sysDate, "yyyy-MM-dd HH:mm:ss");
    }
    
    public static String getOptDate(final Date date) {
        return dateToString(date, "yyyy-MM-dd HH:mm:ss");
    }
    
    public static String getSysOptDate(final String strFormat) {
        final Calendar date = Calendar.getInstance();
        final Date sysDate = date.getTime();
        return dateToString(sysDate, strFormat);
    }
    
    public static String dateToString(final Date dteValue, final String strFormat) {
        if (StringUtils.isEmpty(dteValue)) {
            return null;
        }
        final SimpleDateFormat clsFormat = new SimpleDateFormat(strFormat);
        return clsFormat.format(dteValue);
    }
    
    public static Date stringToDate(String strValue) {
        if (StringUtils.isEmptyAll(strValue)) {
            return null;
        }
        SimpleDateFormat clsFormat = null;
        if (strValue.length() > 19) {
            strValue = strValue.substring(0, 19);
        }
        if (strValue.length() == 19) {
            clsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        else if (strValue.length() == 16) {
            clsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        }
        else if (strValue.length() == 13) {
            clsFormat = new SimpleDateFormat("yyyy-MM-dd HH");
        }
        else if (strValue.length() == 10) {
            clsFormat = new SimpleDateFormat("yyyy-MM-dd");
        }
        else if (strValue.length() == 14) {
            clsFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        }
        else {
            if (strValue.length() != 8) {
                throw new IllegalArgumentException("\u672a\u627e\u5230\u5339\u914d\u7684\u65f6\u95f4\u683c\u5f0f");
            }
            clsFormat = new SimpleDateFormat("yyyyMMdd");
        }
        final ParsePosition pos = new ParsePosition(0);
        return clsFormat.parse(strValue, pos);
    }
    
    public static boolean kuaYue(final String startAdt, final String endAdt) {
        boolean his = false;
        try {
            final Calendar cal = Calendar.getInstance();
            cal.setTime(stringToDate(startAdt));
            cal.set(5, 1);
            final Calendar endCal = Calendar.getInstance();
            endCal.setTime(stringToDate(endAdt));
            endCal.set(5, 1);
            final SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
            final String startMonth = format.format(cal.getTime());
            final String endMonth = format.format(endCal.getTime());
            his = !startMonth.equals(endMonth);
        }
        catch (Exception e) {
            DateUtils.log.error("\u5f02\u5e38", (Throwable)e);
        }
        return his;
    }
    
    public static String getCurDate(Date date) {
        if (date == null) {
            date = new Date();
        }
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
    
    public static String getCurDateForNull(final Date date) {
        if (date == null) {
            return "";
        }
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
    
    public static String getCurTime(Date date) {
        if (date == null) {
            date = new Date();
        }
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
    
    public static String getHHMM(Date date) {
        if (date == null) {
            date = new Date();
        }
        final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(date);
    }
    
    public static String getHHMMSS(Date date) {
        if (date == null) {
            date = new Date();
        }
        final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(date);
    }
    
    public static Date addTime(final Date date, final String unit, final int length) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if ("01".equals(unit)) {
            calendar.add(11, length);
        }
        else if ("02".equals(unit)) {
            calendar.add(5, length);
        }
        else if ("03".equals(unit)) {
            calendar.add(4, length);
        }
        else if ("04".equals(unit)) {
            calendar.add(2, length);
        }
        else if ("05".equals(unit)) {
            calendar.add(12, length);
        }
        return calendar.getTime();
    }
    
    public static Date addTime(final String unit, final int length) {
        final Date date = new Date();
        return addTime(date, unit, length);
    }
    
    public static String getCostCyc(final String costCyc) {
        final Date date = new Date();
        String dateStr = "";
        if ("01".equals(costCyc)) {
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            dateStr = sdf.format(date) + "01";
        }
        else if ("02".equals(costCyc)) {
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            final int month = calendar.get(2);
            final int year = calendar.get(1);
            if (month <= 2) {
                dateStr = year + "0101";
            }
            else if (month <= 5) {
                dateStr = year + "0401";
            }
            else if (month <= 8) {
                dateStr = year + "0701";
            }
            else {
                dateStr = year + "1001";
            }
        }
        else if ("03".equals(costCyc)) {
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            final int year2 = calendar.get(1);
            dateStr = year2 + "0101";
        }
        return dateStr;
    }
    
    public static String getCostCycDate(final String costCyc) {
        final Date date = new Date();
        String dateStr = "";
        if ("01".equals(costCyc)) {
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            dateStr = sdf.format(date) + "-01";
        }
        else if ("02".equals(costCyc)) {
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            final int month = calendar.get(2);
            final int year = calendar.get(1);
            if (month <= 2) {
                dateStr = year + "-01-01";
            }
            else if (month <= 5) {
                dateStr = year + "-04-01";
            }
            else if (month <= 8) {
                dateStr = year + "-07-01";
            }
            else {
                dateStr = year + "-10-01";
            }
        }
        else if ("03".equals(costCyc)) {
            final Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            final int year2 = calendar.get(1);
            dateStr = year2 + "-01-01";
        }
        return dateStr;
    }
    
    public static Date getSuccCyc(final Date date, final BigDecimal succCycLong, final String succCyc) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        final int num = 0 - succCycLong.intValue();
        if ("01".equals(succCyc)) {
            calendar.add(12, num);
        }
        else if ("02".equals(succCyc)) {
            calendar.add(11, num);
        }
        else if ("03".equals(succCyc)) {
            calendar.add(5, num);
        }
        return calendar.getTime();
    }
    
    public static String interval(final Date beginDate, final Date endDate) {
        return changeDateTimeToDHMS(endDate.getTime() - beginDate.getTime());
    }
    
    public static String changeDateTimeToDHMS(final long ms) {
        final Integer ss = 1000;
        final Integer mi = ss * 60;
        final Integer hh = mi * 60;
        final Integer dd = hh * 24;
        final Long day = ms / dd;
        final Long hour = (ms - day * dd) / hh;
        final Long minute = (ms - day * dd - hour * hh) / mi;
        final Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        final Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
        final StringBuffer sb = new StringBuffer();
        if (day > 0L) {
            sb.append(day + "\u5929");
        }
        if (hour > 0L) {
            sb.append(hour + "\u5c0f\u65f6");
        }
        if (minute > 0L) {
            sb.append(minute + "\u5206");
        }
        if (second > 0L) {
            sb.append(second + "\u79d2");
        }
        if (milliSecond > 0L) {
            sb.append(milliSecond + "\u6beb\u79d2");
        }
        return sb.toString();
    }
    
    public static String getCurDateTimeNoSep() {
        return DateUtils.FORMAT_NO_SEP_ON_SECOND.format(new Date());
    }
    
    public static String getCurDateMinuteNoSep() {
        return DateUtils.FORMAT_NO_SEP_ON_MINUTE.format(new Date());
    }
    
    public static String DateToDayStr(final Date date) {
        return DateUtils.ISO_ON_DATE_FORMAT.format(date);
    }
    
    public static String DateToDayTimeStr(final Date date) {
        return DateUtils.DEFAULT_ON_SECOND_FORMAT.format(date);
    }
    
    public static BigDecimal getIntervalDays(final Date beginDate, final Date endDate) {
        long intervalDays = 0L;
        intervalDays = (getCurYMD(endDate).getTime() - getCurYMD(beginDate).getTime()) / 86400000L;
        return BigDecimal.valueOf(intervalDays);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)DateUtils.class);
        MONTH_LENGTH = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        ISO_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
        ISO_ON_SECOND_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ");
        ISO_ON_DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
        ISO_ON_MONTH_FORMAT = FastDateFormat.getInstance("yyyy-MM");
        DEFAULT_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS");
        DEFAULT_ON_SECOND_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        FORMAT_NO_SEP_ON_SECOND = FastDateFormat.getInstance("yyyyMMddHHmmss");
        FORMAT_NO_SEP_ON_MINUTE = FastDateFormat.getInstance("yyyyMMddHHmm");
        FORMAT_NO_SEP_ON_DAY = FastDateFormat.getInstance("yyyyMMdd");
    }
}
